#include "seeker.h"
//segment tree
seeker::seeker(short m, short n)
{
count=0;
a=m;
b=n;
    if(n-m>0){
        left=new seeker(m, (m+n)/2);
        right=new seeker((m+n)/2+1,n);
    }
}

bool seeker::isfull(){
    return count==(b-a+1);
}

bool seeker::isleaf(){
    return (a==b);
}

bool seeker::isempty(){
    return count==0;
}

bool seeker::add(short n){
    if(isfull())return false;
    if(isleaf()){
        ++count;
        return true;
    }
    else if(n >= left->a && n <=left->b){
           bool added=left->add(n);
           if(added)++count;
           return added;
        }
    else if(n >= right->a && n <=right->b){
            bool added=right->add(n);
            if(added)++count;
            return added;
        }
    else throw "invalid index";
}

bool seeker::remove(short n){
    if(isempty())return false;
    if(isleaf()){
        --count;
        return true;
    }
    else if(n >= left->a && n <=left->b){
           bool removed=left->remove(n);
           if(removed)++count;
           return removed;
        }
    else if(n >= right->a && n <=right->b){
            bool removed=right->remove(n);
            if(removed)++count;
            return removed;
        }
     else throw "invalid index";
}

